/**
 * Created by ZhangGong on 2017/7/1.
 */


/*
 * Slider 轮播图滑动组件
 * nav 导航栏盒子
 * */
var Slider = function (box,nav,t) {
    var imgs=box.getElementsByTagName('img')
    var a = [],imgWidth=imgs[0].offsetWidth;//建立导航
    var history = 0,time= 0;
    for (var i = 0; i < imgs.length; i++) {
        a[i] = document.createElement('a');
        nav.appendChild(a[i]);
    }


    a[history].className = 'on';
    a.forEach(function (el, index) {
        el.onmouseover = function () {
            clearInterval(time);
            if (history !== index) {
                a[history].className = '';
            }
            el.className = 'on';
            history = index;
            box.style.left = '-' + index * imgWidth + 'px';
        };
        el.onmouseout = function () {
            timeCount();
        };
    });
    if(!t){t=5000};
    var timeCount = function () {
        time = setInterval(function () {
            if (history < a.length - 1) {
                a[history].className = '';
                box.style.left = '-' + (++history) * imgWidth + 'px';
                a[history].className = 'on';
            } else {
                a[history].className = '';
                history = 0;
                box.style.left = '0px';
                a[history].className = 'on';
            }
        }, t)
    };
    timeCount();
}
window.Slider=Slider;